//
//  WKTabBarController.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/4.
//  Copyright © 2018年 何文虎. All rights reserved.
//

#import "WKTabBarController.h"
#import "WKPushTabBarVC.h"
#import "WKFriendHomeVC.h"
#import "WPlanHomeVC.h"
#import "WKMineListVC.h"
#import "WKPushHomeVC.h"
#import "WKPopupMenu.h"
#import "WKSelfieView.h"
#import "WKDetectionViewController.h"
#import "WKPublishBeautySayVC.h"
#import "WKPublishBeautySayVC.h"
#import "WKPublishFolloVideoVC.h"
#import "WKHomePageVC.h"
#import "WKPublishQuestionVC.h"
#import "WKFaceWatingVC.h"


@interface WKTabBarController ()<AxcAE_TabBarDelegate>

@end

@implementation WKTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self addChildViewControllers];
    
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTabarIdenx) name:@"CHANGETABRINDENX" object:nil];
}

- (void)addChildViewControllers{
   
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:[WKHomePageVC new]];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:[WKFriendHomeVC new]];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:[WKPushHomeVC new]];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:[WPlanHomeVC new]];
    UINavigationController *nav5= [[UINavigationController alloc]initWithRootViewController:[WKMineListVC new]];
    

    NSArray <NSDictionary *>*VCArray =
    @[@{@"vc":nav1,@"normalImg":@"homePage",@"selectImg":@"homePage_select",@"itemTitle":@"美说"},
      @{@"vc":nav2,@"normalImg":@"task",@"selectImg":@"task_select",@"itemTitle":@"美友"},
      @{@"vc":nav3,@"normalImg":@"complaint",@"selectImg":@"complaint",@"itemTitle":@""},
      @{@"vc":nav4,@"normalImg":@"home_activity",@"selectImg":@"home_activity_select",@"itemTitle":@"计划"},
      @{@"vc":nav5,@"normalImg":@"me",@"selectImg":@"me_select",@"itemTitle":@"我的"}];
    // 1.遍历这个集合
    // 1.1 设置一个保存构造器的数组
    NSMutableArray *tabBarConfs = @[].mutableCopy;
    // 1.2 设置一个保存VC的数组
    NSMutableArray *tabBarVCs = @[].mutableCopy;
    [VCArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 2.根据集合来创建TabBar构造器
        AxcAE_TabBarConfigModel *model = [AxcAE_TabBarConfigModel new];
        // 3.item基础数据三连
        model.itemTitle = [obj objectForKey:@"itemTitle"];
        model.selectImageName = [obj objectForKey:@"selectImg"];
        model.normalImageName = [obj objectForKey:@"normalImg"];
        model.normalColor = UIColorFromHex(0x3c3c3c);
        model.selectColor = BACK_COLOR;
        if (idx == 2 ) { // 如果是中间的
            // 设置凸出
            model.bulgeStyle = AxcAE_TabBarConfigBulgeStyleCircular;
            // 设置成纯文字展示
            model.itemLayoutStyle = AxcAE_TabBarItemLayoutStylePicture;
            // 设置大小/边长
            model.itemSize = CGSizeMake(60 ,60); // 自动以长款中最大值进行切圆
        }
        // 备注 如果一步设置的VC的背景颜色，VC就会提前绘制驻留，优化这方面的话最好不要这么写
        // 示例中为了方便就在这写了
        UIViewController *vc = [obj objectForKey:@"vc"];
        vc.view.backgroundColor = [UIColor whiteColor];
        

        // 5.将VC添加到系统控制组
        [tabBarVCs addObject:vc];
        // 5.1添加构造Model到集合
        [tabBarConfs addObject:model];
    }];
    // 使用自定义的TabBar来帮助触发凸起按钮点击事件
    
//    WKPushTabBarVC *testTabBar = [WKPushTabBarVC new];
//    [self setValue:testTabBar forKey:@"tabBar"];
    
    
    // 5.2 设置VCs -----
    // 一定要先设置这一步，然后再进行后边的顺序，因为系统只有在setViewControllers函数后才不会再次创建UIBarButtonItem，以免造成遮挡
    // 大意就是一定要让自定义TabBar遮挡住系统的TabBar
    self.viewControllers = tabBarVCs;
    //////////////////////////////////////////////////////////////////////////
    // 注：这里方便阅读就将AE_TabBar放在这里实例化了 使用懒加载也行
    // 6.将自定义的覆盖到原来的tabBar上面
    // 这里有两种实例化方案：
    // 6.1 使用重载构造函数方式：
    //    self.axcTabBar = [[AxcAE_TabBar alloc] initWithTabBarConfig:tabBarConfs];
    // 6.2 使用Set方式：
    self.axcTabBar = [[AxcAE_TabBar alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, TabBarHeight)];
    self.axcTabBar.tabBarConfig = tabBarConfs;
    // 7.设置委托
    self.axcTabBar.delegate = self;
    self.axcTabBar.backgroundColor = [UIColor clearColor];
//    self.axcTabBar.backgroundColor = [UIColor redColor];

     self.axcTabBar.backgroundImageView.image = [UIImage imageNamed:@"tb_bgimg"];
    // 8.添加覆盖到上边
    [self.tabBar addSubview:self.axcTabBar];
    [self addLayoutTabBar]; // 10.添加适配
    
    
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, ScreenWidth, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    self.axcTabBar.effectView.hidden = YES;
    self.tabBar.backgroundColor = [UIColor clearColor];
    

}
// 9.实现代理，如下：
- (void)axcAE_TabBar:(AxcAE_TabBar *)tabbar selectIndex:(NSInteger)index{
  
    if (index==2) {
        @weakify(self);
        [WKPopupMenu showWKPopupMenuCompletion:^(NSInteger index) {
            @strongify(self);
            NSLog(@"%ld",index);
            [self presentViewControllerWithIndenx:index];
        } cancletion:^{
            
        }];
        return;
    }
    
    
    // 通知 切换视图控制器
    [self setSelectedIndex:index];
    // 自定义的AE_TabBar回调点击事件给TabBarVC，TabBarVC用父类的TabBarController函数完成切换
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    if(self.axcTabBar){
        self.axcTabBar.selectIndex = selectedIndex;
    }
}

// 10.添加适配
- (void)addLayoutTabBar{
    // 使用重载viewDidLayoutSubviews实时计算坐标 （下边的 -viewDidLayoutSubviews 函数）
    // 能兼容转屏时的自动布局
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.axcTabBar.frame = self.tabBar.bounds;
    [self.axcTabBar viewDidLayoutItems];
}


#pragma mark - 点击凸起来那按钮的事件
- (void)presentViewControllerWithIndenx:(NSInteger)indenx{
    if (indenx==0) {
        [WKSelfieView showWKSelfieViewTakeCamera:^{
//            WKFaceWatingVC *dvc = [[WKFaceWatingVC alloc]init];
           WKDetectionViewController* dvc = [[WKDetectionViewController alloc] init];
            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc];
            navi.navigationBarHidden = true;
            [self presentViewController:navi animated:YES completion:nil];
        }];
    }
    if (indenx==1) {
        WKPublishBeautySayVC *beautysayVC = [[WKPublishBeautySayVC alloc]init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:beautysayVC];
        [self presentViewController:navi animated:YES completion:nil];
    }
    
    if (indenx==2) {
        WKPublishFolloVideoVC *videoVC = [[WKPublishFolloVideoVC alloc]init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:videoVC];
        [self presentViewController:navi animated:YES completion:nil];
    }
    
    if (indenx ==3) {
        WKPublishQuestionVC *questionVC = [[WKPublishQuestionVC alloc]init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:questionVC];
        [self presentViewController:navi animated:YES completion:nil];
    }
    
}

- (void)changeTabarIdenx{
    [self setSelectedIndex:1];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"CHANGETABRINDENX" object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
